package com.ymttest.database.sqlwapper.shenqisettlement;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.shenqisettlement.OrderSettlementDetail;

public class OrderSettlementDetailWrapper {

	SqlManager manager;

    public OrderSettlementDetailWrapper() {
        manager = new SqlManager("shenqisettlement");
    }
    
    @SuppressWarnings("unchecked")
	public List<OrderSettlementDetail> selectByOrderId(long orderId) {
    	
    	List<OrderSettlementDetail> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectByOrderId",
                        orderId);
        manager.commit();
        
		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
		}
		
		return result;
    }
    
    @SuppressWarnings("unchecked")
	public List<OrderSettlementDetail> selectAllOrderByOrderId(long orderId) {
    	
    	List<OrderSettlementDetail> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectAllOrderByOrderId",
                        orderId);
        manager.commit();
        
		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
		}
		
		return result;
    }
    
    @SuppressWarnings("unchecked")
	public OrderSettlementDetail selectByOrderIdAndUserId(long orderId, long userId) {
    	
    	Map<String, Object> m = new HashMap<String, Object>();
		m.put("user_id", userId);
		m.put("order_id", orderId);
    	
    	List<OrderSettlementDetail> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectByOrderIdAndUserId",
                        m);
        manager.commit();
        
		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
		}
		
		return result.get(0);
    }
    
    @SuppressWarnings("unchecked")
	public OrderSettlementDetail selectRefundedByOrderIdAndUserId(long orderId, long userId, String bizId) {
    	
    	Map<String, Object> m = new HashMap<String, Object>();
		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("biz_id", bizId);
    	
    	List<OrderSettlementDetail> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectRefundedByOrderIdAndUserId",
                        m);
        manager.commit();
        
		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
		}
		
		return result.get(0);
    }
    
    @SuppressWarnings("unchecked")
	public OrderSettlementDetail selectCollectingForOthersByOrderIdAndUserId(long orderId, long userId) {
    	
    	Map<String, Object> m = new HashMap<String, Object>();
    	
		m.put("user_id", userId);
		m.put("order_id", orderId);
    	
    	List<OrderSettlementDetail> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectCollectingForOthersByOrderIdAndUserId",
                        m);
        manager.commit();
        
		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
			
			return null;
		}
		
		return result.get(0);
    }
    
    @SuppressWarnings("unchecked")
	public OrderSettlementDetail selectRefundedCollectingForOthersByOrderIdAndUserId(long orderId, long userId, String bizId) {
    	
    	Map<String, Object> m = new HashMap<String, Object>();
    	
		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("biz_id", bizId);
    	
    	List<OrderSettlementDetail> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectRefundedCollectingForOthersByOrderIdAndUserId",
                        m);
        manager.commit();
        
		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
			
			return null;
		}
		
		return result.get(0);
    }
    
    @SuppressWarnings("unchecked")
	public OrderSettlementDetail selectRefundedFailedCollectingForOthersByOrderIdAndUserId(long orderId, long userId, String bizId) {
    	
    	Map<String, Object> m = new HashMap<String, Object>();
    	
		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("biz_id", bizId);
    	
    	List<OrderSettlementDetail> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectRefundedFailedCollectingForOthersByOrderIdAndUserId",
                        m);
        manager.commit();
        
		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
			
			return null;
		}
		
		return result.get(0);
    }
    
    @SuppressWarnings("unchecked")
	public OrderSettlementDetail selectPromotersDetailByOrderIdAndUserId(long orderId, long userId, int settlementType) {
    	
    	if (settlementType != 5 && settlementType != 6)
    		return null;
    	
    	Map<String, Object> m = new HashMap<String, Object>();
		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("settlement_type", settlementType);
    	
    	List<OrderSettlementDetail> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectPromotersDetailByOrderIdAndUserId",
                        m);
        manager.commit();
        
		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
			
			return null;
		}
		
		return result.get(0);
    }
    
	@SuppressWarnings("unchecked")
	public OrderSettlementDetail selectPromotersDetailByOrderIdAndUserIdAndProxyFissonId(
			long orderId, long userId, long proxyFissonId, int settlementType, String bizId) {

		Map<String, Object> m = new HashMap<String, Object>();
		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("fission_proxy_id", proxyFissonId);
		m.put("settlement_type", settlementType);
		m.put("biz_id", bizId);

		List<OrderSettlementDetail> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectPromotersDetailByOrderIdAndUserIdAndProxyFissonId",
						m);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");

			return null;
		}

		return result.get(0);
	}
    
	@SuppressWarnings("unchecked")
	public List<OrderSettlementDetail> selectRefundedByOrderId(long orderId) {

		List<OrderSettlementDetail> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectRefundedByOrderId",
						orderId);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
		}

		return result;
	}
    
    public int insert(OrderSettlementDetail detail) {
    	
		int result = manager
				.insert("com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.insert",
						detail);
    	manager.commit();
    	
    	if (result == 0)
    		Logger.comment("插入数据失败");
    	
    	return result;
    }
    
    public int UpdateSettlementAmountByOrderIdAndUserId(Long orderId, Long userId, BigDecimal settlementAmount) {
    	
    	Map<String, Object> m = new HashMap<String, Object>();
    	
		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("settlement_amount", settlementAmount);
    	
		int result = manager
				.insert("com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.UpdateSettlementAmountByOrderIdAndUserId",
						m);
    	manager.commit();
    	
    	if (result == 0)
    		Logger.comment("插入数据失败");
    	
    	return result;
    }
    
    public int UpdateTotalRMBAmountByOrderIdAndUserId(Long orderId, Long userId, BigDecimal totalRMBAmount) {
    	
    	Map<String, Object> m = new HashMap<String, Object>();
    	
		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("total_rmb_amount", totalRMBAmount);
    	
		int result = manager
				.insert("com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.UpdateTotalRMBAmountByOrderIdAndUserId",
						m);
    	manager.commit();
    	
    	if (result == 0)
    		Logger.comment("插入数据失败");
    	
    	return result;
    }

	@SuppressWarnings("unchecked")
	public OrderSettlementDetail selectDistributorByOrderIdAndUserId(
			Long orderId, Long userId) {

		Map<String, Object> m = new HashMap<String, Object>();
		m.put("user_id", userId);
		m.put("order_id", orderId);

		List<OrderSettlementDetail> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectDistributorByOrderIdAndUserId",
						m);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
			return null;
		}

		return result.get(0);
	}

	@SuppressWarnings("unchecked")
	public OrderSettlementDetail selectRefundedDistributorByOrderIdAndUserId(
			Long orderId, Long userId, String bizId) {

		Map<String, Object> m = new HashMap<String, Object>();

		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("biz_id", bizId);

		List<OrderSettlementDetail> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectRefundedDistributorByOrderIdAndUserId",
						m);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
			return null;
		}

		return result.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public OrderSettlementDetail selectPartnerDistributorByOrderIdAndUserId(
			Long orderId, Long userId) {

		Map<String, Object> m = new HashMap<String, Object>();
		m.put("user_id", userId);
		m.put("order_id", orderId);

		List<OrderSettlementDetail> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectPartnerDistributorByOrderIdAndUserId",
						m);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
			return null;
		}

		return result.get(0);
	}

	@SuppressWarnings("unchecked")
	public OrderSettlementDetail selectRefundedPartnerDistributorByOrderIdAndUserId(
			Long orderId, Long userId, String bizId) {

		Map<String, Object> m = new HashMap<String, Object>();

		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("biz_id", bizId);

		List<OrderSettlementDetail> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectRefundedPartnerDistributorByOrderIdAndUserId",
						m);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
			return null;
		}

		return result.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public OrderSettlementDetail selectRefundFailedByOrderIdAndUserId(long orderId, long userId, String bizId) {
    	
    	Map<String, Object> m = new HashMap<String, Object>();
    	
		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("biz_id", bizId);
    	
    	List<OrderSettlementDetail> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectRefundFailedByOrderIdAndUserId",
                        m);
        manager.commit();
        
		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
			
			return null;
		}
		
		return result.get(0);
    }
	
	public int updateGoodsOwnerSettlementExpectedTimeByOrderIdAndUserId(Long orderId, Long userId, String goodsOwnerSettlementExpectedTime) {
    	
    	Map<String, Object> m = new HashMap<String, Object>();
    	
		m.put("user_id", userId);
		m.put("order_id", orderId);
		m.put("goods_owner_settlement_expected_time", goodsOwnerSettlementExpectedTime);
    	
		int result = manager
				.insert("com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.updateGoodsOwnerSettlementExpectedTimeByOrderIdAndUserId",
						m);
    	manager.commit();
    	
    	if (result == 0)
    		Logger.comment("插入数据失败");
    	
    	return result;
    }
	
	@SuppressWarnings("unchecked")
	public OrderSettlementDetail selectSecondConfirmUnpaidOrderByOrderIdAndUserId(
			long orderId, long userId) {

		Map<String, Object> m = new HashMap<String, Object>();
		m.put("user_id", userId);
		m.put("order_id", orderId);

		List<OrderSettlementDetail> result = manager
				.getSelectResults(
						"com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.selectSecondConfirmUnpaidOrderByOrderIdAndUserId",
						m);
		manager.commit();

		if (result.size() == 0) {
			Logger.comment("order_settlement_detail没有数据");
		}

		return result.get(0);
	}


	public int updateShengpaySharingExecuteStatusByOrderId(Long orderId, int shengPaySharingExecuteStatus,
														   int settlementType) {

		Map<String, Object> m = new HashMap<String, Object>();

		m.put("order_id", orderId);
		m.put("shengpay_sharing_execute_status", shengPaySharingExecuteStatus);
		m.put("settlement_type", settlementType);

		int result = manager
				.insert("com.ymttest.database.mapping.shenqisettlement.OrderSettlementDetailMapper.updateShengpaySharingExecuteStatusByOrderId",
						m);
		manager.commit();

		if (result == 0)
			Logger.comment("更新数据失败");

		return result;
	}
	
    public static void main(String[] args) {
    	
    	OrderSettlementDetailWrapper test = new OrderSettlementDetailWrapper();
    	
//    	OrderSettlementDetail detail = new OrderSettlementDetail();
//    	
//    	detail.setOrder_id(1151418699L);
//    	detail.setUser_id(2490L);
//    	detail.setTotal_rmb_amount(BigDecimal.TEN);
//    	detail.setTotal_foreign_amount(BigDecimal.ONE);
//    	detail.setForeign_currency("USD");
//    	detail.setExchange_rate("1.169590643274853801169590643");
//    	detail.setPlatform_fee_rate(BigDecimal.ZERO);
//    	detail.setPlatform_fee(BigDecimal.ONE);
//    	detail.setSettlement_currency("USD");
//    	detail.setSettlement_amount(new BigDecimal("9.00"));
//    	detail.setSettlement_status(1);
//    	detail.setSettlement_time(YMTDateUtil.getDate());
//    	detail.setIs_rollback(false);
//    	detail.setIs_proxy_product(true);
//    	detail.setGoods_owner_id(2490L);
//    	detail.setParent_proxy_id(null);
//    	detail.setUser_role("GOODS_OWNER");
//    	detail.setOrder_create_time(YMTDateUtil.getDate());
//    	detail.setRemark("autotest");
//    	detail.setIs_personal_applet(false);
//    	detail.setAdd_time(YMTDateUtil.getDate());
//    	detail.setUpdate_time(YMTDateUtil.getDate());
    	
		System.out.println(test
				.updateShengpaySharingExecuteStatusByOrderId(
						14331110317L, 1, 0));
    }
}
